//
//  CompanyNewsViewController.m
//  NewPro
//
//  Created by RYX on 2018/10/31.
//  Copyright © 2018年 何荧红. All rights reserved.
//

#import "CompanyNewsViewController.h"
#import "CompanyNewsCell.h"
#import "CompanyHelpStore.h"
#import "wkViewController.h"
#import "CompanyNewsModel.h"
#import "HomeList.h"

@interface CompanyNewsViewController ()<UITableViewDelegate,UITableViewDataSource,DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,copy) NSArray *listArr;

@end

@implementation CompanyNewsViewController

- (UITableView *)tableView{
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.5)];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
        
        MJWeakSelf
        MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            
            [weakSelf refresh];
        }];
        
        header.lastUpdatedTimeLabel.hidden = YES;
        _tableView.mj_header = header;
    }
    
    return _tableView;
}


- (void)refresh{
    
    CompanyHelpStore *store = [[CompanyHelpStore alloc] init];
    
    MJWeakSelf
    [store getNewsListSuccess:^(NSArray *listArr) {
        
        [weakSelf.tableView.mj_header endRefreshing];
        weakSelf.listArr = listArr;
        [weakSelf.tableView reloadData];
        
    } Failure:^(NSError *error) {
        [weakSelf.tableView.mj_header endRefreshing];
    }];
}
- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self setNavBarWithTitle:@"企业辅助"];
    
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(self.view);
    }];

    [self.tableView.mj_header beginRefreshing];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [CompanyNewsCell getHeight];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.listArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CompanyNewsCell *cell = [CompanyNewsCell tempWithTableView:tableView];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor clearColor];
    cell.model = self.listArr[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    wkViewController *VC = [[wkViewController alloc] init];
    [HomeList sharedInstance].selectedNew = [CompanyNewsModel mj_objectWithKeyValues:self.listArr[indexPath.row]];
    [VC setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:VC animated:YES];
}

@end
